<?php

class Application_Model_GeneralizeReceive extends Application_Model_DomainObject {

    protected $_id;
    /**
     * 活动Id
     */
    protected $_generalizeId;
    /**
     * 规则Id
     */
    protected $_ruleId;
    /**
     * 用户Id
     */
    protected $_userId;
    /**
     * 创建时间
     */
    protected $_dateCreated;
    /**
     * 领取规则Id
     */
    protected $_receiveId;
    
    public static function addReceive($generalizeId,$ruleId,$userId,$receiveId) {
        $receiveItem = new Application_Model_GeneralizeReceive();
        $receiveItem->generalizeId = $generalizeId;
        $receiveItem->ruleId = $ruleId;
        $receiveItem->userId = $userId;
        $receiveItem->receiveId = $receiveId;
        $receiveItem->dateCreated = date("Y-m-d H:i:s");
        $receiveItem->save();
    }
    public static function getDistinct($generalizeId,$distinct) {
        return self::getDao()->distinct(array("generalizeId = "=>$generalizeId), $distinct);
    }
    
    public static function getFilter($id,$quick,$starttime,$endtime,$grantType,$giftType,$giftId,$pagesize,$pageindex) {
        $where = array();
        $and = "";
        if ($id != "") {
            $where[$and." generalizeId = "] = $id;
            $and = " and ";
        }
        if ($quick != "") {
            $userItem = Application_Model_User::getByNikeNameStore($quick);
            $arrUser = "(";
            foreach ($userItem as $userVal) {
                $arrUser .= $userVal->Id.",";
            }
            $arrUser .= "0)";
            $where[$and." userId in "] = $arrUser;
            $and = " and ";
        }
        if($starttime != "") {
            $where[$and." dateCreated >= "] = $starttime;
            $and = " and ";
        }
        if($endtime != "") {
            $where[$and." dateCreated <= "] = $endtime." 23:59:59";
            $and = " and ";
        }
        if($grantType != "" || $giftType != "" || $giftId != "") {
            $ruleItem = Application_Model_GeneralizeRule::getRuleByGrantTypeGiftTypeId($grantType, $giftType, $giftId);
            $arrRule = "(";
            foreach ($ruleItem as $ruleVal) {
                $arrRule .= $ruleVal->Id.",";
            }
            $arrRule .= "0)";
            $where[$and." ruleId in "] = $arrRule;
            $and = " and ";
        }
        return self::getDao()->queryPage($where, " id desc ", $pagesize, $pageindex);
    }
    
    public function getGeneralizeItem() {
        return Application_Model_Generalize::load($this->_generalizeId);
    }
    
    public function getRuleItem() {
        return Application_Model_GeneralizeRule::load($this->_ruleId);
    }
    
    public function getUserInfo() {
        return Application_Model_User::load($this->_userId);
    }
    
    
    public function getGlHref($grantType) {
        $result = array();
        $item = Application_Model_Generalize::createdReceive($grantType, $this->_receiveId);
        switch ($grantType) {
            case Application_Model_GeneralizeRule::Grant_Way:
                $result["href"] = "/admini/managewaybill/billaccep/id/".$this->_receiveId;
                $result["name"] = $item?$item->getBillNo():"未知";
                break;
            case Application_Model_GeneralizeRule::Grant_Order:
                $result["href"] = "/admini/purchase/buydetail/id/".$this->_receiveId;
                $result["name"] = $item?$item->getBillNo():"未知";
                break;
            case Application_Model_GeneralizeRule::Grant_Visiting:
                $result["href"] = "/admini/visiting/vsdetail/id/".$this->_receiveId;
                $result["name"] = $item?$item->getBillNo():"未知";
                break;
            case Application_Model_GeneralizeRule::Grant_Recharge:
                $result["href"] = "/admini/manageuser/paybill/?quick=".$item->UserInfo->storeno;
                $result["name"] = $item?$item->getBillNo():"未知";
                break;
            case Application_Model_GeneralizeRule::Grant_Share:
                $result["href"] = "javascript:void(0)";
                $result["name"] = "分享";
                break;
            default:
                break;
        }
        return $result;
    }
}